package com.newland.starpos.base;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Page<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private final List<T> data = new ArrayList<T>();

	private final long rows;

    @JsonIgnore
	private final Pageable pageable;

	public Page() {
		this.rows = 0L;
		this.pageable = new Pageable();
	}

	public Page(List<T> content, long rows, Pageable pageable) {
		this.data.addAll(content);
		this.rows = rows;
		this.pageable = pageable;
	}
    @JsonIgnore
	public int getPageNumber() {
		return pageable.getPageNumber();
	}
    @JsonIgnore
	public int getPageSize() {
		return pageable.getPageSize();
	}

    @JsonIgnore
	public int getTotalPages() {
		return (int) Math.ceil((double) getRows() / (double) getPageSize());
	}

	public List<T> getData() {
		return data;
	}

	public long getRows() {
		return rows;
	}

    @JsonIgnore
	public Pageable getPageable() {
		return pageable;
	}

	public static final <T> Page<T> emptyPage(Pageable pageable) {
		return new Page<T>(Collections.<T> emptyList(), 0L, pageable);
	}

}